<?php

namespace App\Admin\Controllers;

use App\Models\Express;
use App\Models\Order;
use App\Http\Controllers\Controller;
use App\Models\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use EasyWeChat;
use DB;

class Orders2Controller extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('待归还订单列表');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show(Order $order)
    {
        return Admin::content(function (Content $content) use ($order) {
            $content->header('查看订单');

            $user = User::query()->where('id',$order->user_id)->first();
            $days = 0;
            if(!empty($user) && $user->is_vip == 1)
            {
                if(!empty($user->vip_end_time) && !empty($user->vip_start_time))
                {
                    $now = time();
                    if($user->vip_end_time >= $now)
                    {
                        $timediff = $user->vip_end_time - $now;
                        $days = ceil($timediff/86400);
                    }
                }
                else
                {
                    $days = DB::table('user_vip_orders')->where('user_id',$user->id)->where('is_used',0)->where('status',1)->sum('vip_days');
                }
            }
            $order->vip_days = $days;
            $content->body(view('admin.orders.show', ['order' => $order]));
            //$content->description('description');

            /*$content->body(Admin::show(Order::findOrFail($id), function (Show $show) {

                $show->id();

                $show->created_at();
                $show->updated_at();
            }));*/
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->model()->whereIn('status', [1,2]);
            $grid->model()->orderBy('id', 'desc');

            //$grid->id('ID')->sortable();
            $grid->no('订单流水号');
            $grid->column('user.wechat_nickname', '用户昵称');
            $grid->column('user.telephone', '用户绑定手机号');

            // 不存的字段列
            $grid->column('vip_days','会员剩余天数')->display(function () {
                $user = User::query()->where('id',$this->user_id)->first();
                $days = 0;
                if(!empty($user) && $user->is_vip == 1)
                {
                    if(!empty($user->vip_end_time) && !empty($user->vip_start_time))
                    {
                        $now = time();
                        if($user->vip_end_time >= $now)
                        {
                            $timediff = $user->vip_end_time - $now;
                            $days = ceil($timediff/86400);
                        }
                    }
                    else
                    {
                        $days = DB::table('user_vip_orders')->where('user_id',$user->id)->where('is_used',0)->where('status',1)->sum('vip_days');
                    }
                }
                return $days;
            });

            $grid->status('状态')->display(function($value) {
                if($value == 1)
                {
                    return '待收货';
                }
                else
                {
                    return '借阅中';
                }
            });
            $grid->back_status('归还状态')->display(function($value) {
                return Order::$backStatusMap[$value];
            });

            $grid->created_at('下单时间');

            // 禁用创建按钮，后台不需要创建订单
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableFilter();

            $grid->actions(function ($actions) {
                // 禁用删除和编辑按钮
                $actions->disableDelete();
                $actions->disableEdit();
            });

            /*$grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });*/
        });
    }

    public function ship(Order $order, Request $request)
    {
        $data = $this->validate($request, [
            'express_company' => ['required'],
            'express_no'      => ['required'],
        ], [], [
            'express_company' => '物流公司',
            'express_no'      => '物流单号',
        ]);

        //var_dump($data);die;
        $express = Express::query()->find($data['express_company']);

        $ret = $order->update([
            // 我们在 Order 模型的 $casts 属性里指明了 ship_data 是一个数组
            // 因此这里可以直接把数组传过去
            'express_name' => $express->name,
            'express_code' => $express->code,
            'express_no' => $data['express_no'],
        ]);

        // 返回上一页
        return redirect()->back();
    }
}
